from django.core.paginator import Paginator, EmptyPage
from django.http import HttpResponseBadRequest
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse
from django.views import View
from home.models import Article,ArticleCategory
#首页
class IndexView(View):
    #     """首页广告"""
    def get(self,request):
        """提供首页广告界面"""
        cat_id = request.GET.get('cat_id',1)
        try:
            category = ArticleCategory.objects.get(id=cat_id)
        except ArticleCategory.DoesNotExist:
            return HttpResponseBadRequest('信息不存在')
        #         #?cat_id=xxx&page_num=xxx&page_size=xxx
        page_index = request.GET.get('page_index',1)
        page_size = request.GET.get('page_size',2)
        # 分页数据
        articles = Article.objects.filter(category_id=cat_id)
        from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
        # 创建分页器：每页N条记录
        pages = Paginator(articles,page_size)
        # 判断分类id
        try:
            list= pages.page(page_index)
        except PageNotAnInteger:
            list = pages.page(1)
        except EmptyPage:
            # 获取列表页总页数
            list = pages.page(pages.num_pages)
        hot_pl = Article.objects.filter(category=category).order_by('-comments_count').distinct()[:2]
            #组织数据传递给前端
        #组织数据传递给模板
        context = {
            'category' : category,
            'articles' : list,
            'cat_id' : cat_id,
            'hot_pl':hot_pl,
        }
        return render(request,'index.html', context=context)
#详情页面展示
class DetailView(View):
    def get(self,request):
        art_id = request.GET.get('art_id')
        arties = Article.objects.all()

        try:
            articlies = Article.objects.get(id=art_id)
        except Article.DoesNotExist:
            return render(request,'404.html')

        #浏览量简单写法
        articlies.total_views += 1
        articlies.save()

        from home.models import Comment

        comments = Comment.objects.filter(article=articlies).order_by('-created_time')
        li = Article.objects.filter(author=articlies.author)[:3]
        context={
           'articlies':articlies,
            'arties':arties,
            'comments':comments,
            'list':li,
        }
        return render(request,'detail.html',context=context)

    def post(self,request):
        #1.获取已经登录用户信息
        user = request.user
        #2.判断用户是否登录
        if user and user.is_authenticated:
            #3.登录用户才可以接收form数据
            #3-1接收评论数据
            art_id = request.POST.get('art_id')
            content = request.POST.get('content')
            #3-2验证文章是否存在
            try:
                art = Article.objects.get(id=art_id)
            except Article.DoesNotExist:
                return HttpResponseBadRequest("该文章不存在")
            #3-3保存评论数据
            from home.models import Comment
            Comment.objects.create(
                content=content,article=art,user=user
            )
            #3-4修改文章的评论数量
            art.comments_count +=1
            art.save()
            #刷新当前页面
            req_url = request.path + '?art_id=' + art_id
            return redirect(req_url)
        else:
            #4.未登录用户跳转到登录页面
            return redirect(reverse('users:login'))

